package com.udaos.modules.affiche.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.affiche.Ud_affiche;
import com.udaos.models.region.Ud_region;
import com.udaos.models.sys.Sys_user;
/**
 * 公告WEB
 * @author mutaotao
 *
 */
import com.udaos.modules.affiche.services.UdAfficheService;
import com.udaos.modules.region.services.UdRegionService;
@IocBean
@At("/platform/affiche")
@Filters({ @By(type = PrivateFilter.class) })
public class UdAfficheController {
	
	@Inject
	private UdAfficheService afficheService;
	@Inject
	private UdRegionService regionService;
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/affiche/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		Subject subject = SecurityUtils.getSubject();
        Sys_user user = (Sys_user) subject.getPrincipal();
        cnd.and("company_id", "=", user.getUnit().getCompany_id());
		return afficheService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@At
	@Ok("beetl:/platform/affiche/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取片区
		List<Ud_region> regions=afficheService.getRegions();
		map.put("regions", regions);
		return map;
	}

	/**
	 * do新增
	 * @param affiche
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object addDo(@Param("..") Ud_affiche affiche,HttpServletRequest req) {
		try {
			Subject subject = SecurityUtils.getSubject();
			Sys_user user = (Sys_user) subject.getPrincipal();
			affiche.setCompany_id(user.getUnit().getCompany_id());
			affiche.setCreateBy(user.getId());
			affiche.setCreateAt((int)(System.currentTimeMillis()/1000));
			if(affiche.getState()==0){
				affiche.setPutAt((int)(System.currentTimeMillis()/1000));
			}
			afficheService.insert(affiche);
			return Result.success("保存成功");
		} catch (Exception e) {
			System.err.println(e.getMessage());
			return Result.error("保存失败");
		}
	}
	
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/affiche/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取片区
		List<Ud_region> regions=afficheService.getRegions();
		map.put("regions", regions);
		map.put("affiche", afficheService.fetch(id));
		return map;
	}

	/**
	 * do修改
	 * @param affiche
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object editDo(@Param("..") Ud_affiche affiche,HttpServletRequest req) {
		try {
			
			if(affiche.getState()==0 && afficheService.fetch(affiche.getId()).getState()==1){
				affiche.setPutAt((int)(System.currentTimeMillis()/1000));
			}
			afficheService.updateIgnoreNull(affiche);
			return Result.success("修改成功");
		} catch (Exception e) {
			return Result.error("修改失败");
		}
	}
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_affiche affiche = afficheService.fetch(id);
        	affiche.setDelFlag(true);
        	afficheService.updateIgnoreNull(affiche);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	
	/**
	 * 获取片区名称
	 * @param id
	 * @return
	 */
	@At
	@Ok("json")
	public String getRegionName(String id){
		if(!Strings.isBlank(id)){
			return regionService.fetch(id).getName();
		}
		return "";
	}
}
